package cn.wolfcode.crm.service.impl;


import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientRecord;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.ClientDevelopmentPlanMapper;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.ClientRecordMapper;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClientRecordService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClientRecordServiceImpl implements IClientRecordService {
    @Autowired
    private ClientRecordMapper clientRecordMapper;
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private ClientDevelopmentPlanMapper clientDevelopmentPlanMapper;
    public void save(ClientRecord clientRecord) {
        int status = clientRecord.getStatus();
        //查出客户
        Client client1 = clientMapper.selectByPrimaryKey(clientRecord.getClient().getId());
        System.out.println(client1.getId());
        clientRecord.setClient(client1);
        clientRecord.setOldOustomerManager(client1.getCustomerManager());
        //设置当前操作用户
        clientRecord.setHandler((Employee) SecurityUtils.getSubject().getPrincipal());
        //设置时间
        clientRecord.setOperationDate(new Date());
        //获取记录的记录类型,用来分配操作
        //转移操作
        if(status==1){
          clientRecordMapper.insertByClientTransfer(clientRecord);
            //转移完成 改变客户的负责人
            Client client = clientRecord.getClient();
            client.setCustomerManager(clientRecord.getNewOustomerManager());
            clientMapper.updateCustomerManager(client);
        }
        //其余记录
        else {
            clientRecordMapper.insertByClientRecordOeder(clientRecord);
            Client client = clientRecord.getClient();
            //得到关于客户的开发计划
            if(status==2){
                client.setState(2);
                clientMapper.changeClientState(clientRecord.getClient());
                Long developmentPlanID = clientDevelopmentPlanMapper.getDevelopmentPlanIDByClientId(clientRecord.getClient().getId());
                clientDevelopmentPlanMapper.deleteByPrimaryKey(developmentPlanID);
            }
            if(status==3){
                client.setState(-1);
                clientMapper.changeClientState(clientRecord.getClient());
                Long developmentPlanID = clientDevelopmentPlanMapper.getDevelopmentPlanIDByClientId(clientRecord.getClient().getId());
                clientDevelopmentPlanMapper.deleteByPrimaryKey(developmentPlanID);
            }
            //删除有关的开发计划

        }


    }

    public void delete(Long id) {
        clientRecordMapper.deleteByPrimaryKey(id);

    }


    public ClientRecord get(Long id) {
        return clientRecordMapper.selectByPrimaryKey(id);
    }

    public List<ClientRecord> getAll() {

        return null;
    }

    public PageResult<ClientRecord> query(ObjectQuery qo) {
        int totalCount = clientRecordMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<ClientRecord>(totalCount, Collections.<ClientRecord>emptyList());
        }


        return new PageResult<ClientRecord>(totalCount,clientRecordMapper.queryForList(qo));
    }

}
